package cn.dida.util;

import java.util.Properties;
import java.util.ResourceBundle;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.order.OrderProducer;

import cn.dida.model.EntPayEntity;
import cn.dida.model.RedPackEntity;

public class AliyunMqUtil {
	private static OrderProducer producer;
	static{
	    Properties properties = new Properties();
        properties.put(PropertyKeyConst.ProducerId,ResourceBundle.getBundle("app").getString("aliyun.mq.ProducerId"));// 您在控制台创建的 Producer ID
        try {
		properties.put(PropertyKeyConst.AccessKey, new String(Base64.decode((ResourceBundle.getBundle("app").getString("aliyun.mq.AccessKey")))));//解码阿里的AccessKey
        properties.put(PropertyKeyConst.SecretKey, new String(Base64.decode((ResourceBundle.getBundle("app").getString("aliyun.mq.SecretKey")))));// /解码阿里的SecretKey 
        properties.put(PropertyKeyConst.ONSAddr,ResourceBundle.getBundle("app").getString("aliyun.mq.addr"));           
        producer = ONSFactory.createOrderProducer(properties);
        producer.start();
        } catch (Exception e) {
        	
			e.printStackTrace();
		}  
	 }
	/**
	 * 红包发送消息方法
	 * @param redPack
	 * @return 消息ID
	 */
	public static String sendRedPackPayMessage(RedPackEntity redPack){
		String message_id = UUID.randomUUID().toString(); 
		return payMessage(redPack,message_id,ResourceBundle.getBundle("app").getString("aliyun.mq.topic"),"REDPACK");
	}
	
	/**
	 * 企业支付消息发送方法
	 * @param entPay  企业支付实体类
	 * @return 消息ID
	 */
	public static String sendEntPayMessage(EntPayEntity entPay){
		String message_id = UUID.randomUUID().toString();
		return payMessage(entPay,message_id,ResourceBundle.getBundle("app").getString("aliyun.mq.topic"),"ENTPAY");
	}
	
	public static String payMessage(Object payEntity,String message_id,String topic,String tag){
		 Message msg = new Message(topic,tag,JSONObject.toJSONString(payEntity).getBytes());
		 msg.setKey(message_id);
		 String shardingKey = String.valueOf(message_id);
		 SendResult sendResult = producer.send(msg, shardingKey);
		 System.out.println("Message Id:" + sendResult.getMessageId());
		 return  sendResult.getMessageId();
		 
	}
	
	public static void main(String[] args) {
	 
//<<<<<<< HEAD
//        String orderId = UUID.randomUUID().toString();           
//        EntPayEntity entPay=new EntPayEntity();
//        entPay.setAmount(100);
//        entPay.setAppId("wxf75ff9d6ace8f97e");
//        entPay.setDescription("企业付款1元测试");
//        entPay.setMchId("1356931302");
//        entPay.setOpenId("oI4wcs9uP-L1Ab30sn9evFtJ_7So");
//        entPay.setMchKey("7c06f70573db4dd0b2ea8de294772add");
//        System.out.println(AliyunMqUtil.sendEntPayMessage(entPay));
//=======
	            String orderId = UUID.randomUUID().toString();           
/*	            EntPayEntity entPay=new EntPayEntity();
	            for(int i=0;i<1;i++){
	            entPay.setCallBackUrl("http://192.168.1.102:8080/vats-api-lbs/test/jsonString");
	            entPay.setAmount(100);
	            entPay.setAppId("wxf16e8b4d9bbc864a");
	            entPay.setDescription("企业付款1元测试");
	            entPay.setMchId("1356931302");
	            entPay.setOpenId("oI4wcs9uP-L1Ab30sn9evFtJ_7So");
	            entPay.setMchKey("7c06f70573db4dd0b2ea8de294772add");
	            System.out.println(AliyunMqUtil.sendEntPayMessage(entPay));
	            }*/
//	            { "weixin_gzh_secret" : "a6548c339a0168701114c7e70a2a7adc",
//	            	 "weixin_gzh_qp" : "/companyqp/da125cfe387345eb85ec159d54a87b73.jpg",
//	            	 "weixin_mh_pay_key" : "1",
//	            	 "weixin_mh_id" : "1447259002",
//	            	 "weixin_qyh_corpid" : "wxfff7f08ed6f9d673",
//	            	 "weixin_gzh_name" : "华泽集团",
//	            	 "weixin_gzh_appid" : "wx407428f21746abe8",
//	            	 "weixin_qyh_corpsecret" : "KdsAJP_4DxGYOzwv2fbFvkgCI59uQ7ZFZsUrFS6Zmlb-78Pkbu5m1Qmsg0pEEvbX" }
//>>>>>>> refs/remotes/origin/aliyun-mq
	        
	            for(int i=0;i<1;i++){
	            RedPackEntity redpack=new RedPackEntity();
	            redpack.setMchId("1448885502");
	            redpack.setMchKey("wurewjfkj238219cxz7f9ziehrh3inrk");
	            redpack.setAppId("wx54cf0b68998c0b6e");
	            redpack.setReOpenid("obkTf0alRrjiDKBz_kvuj35F7imM");
	            redpack.setClientIp("192.168.1.102");
	            redpack.setTotalAmount(100);
	            redpack.setActName("参加扫码返利活动");
	            redpack.setSendName("金六福一坛好酒");
	            redpack.setTotalNum(1);
	            redpack.setRemark("测试备注");
	            redpack.setWishing("恭喜发财");
	            redpack.setCallBackUrl("http://192.168.1.101:8080/vats-api-lbs/test/jsonString");
	            System.out.println(AliyunMqUtil.sendRedPackPayMessage(redpack));
	            }
	
	}
}
